package test.net.udp.client;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import arduino.AProtocol;
import net.udp.client.UDPClient;

public class testUDPClient{

	
	public static void main(String[] args) {
		testUDPClient t = new testUDPClient();
		
//		UDPClient client = new UDPClient();		
//		Random r = new Random();
//		
//		while(true)
//		{
//			
//		AProtocol a = new AProtocol(40+r.nextInt(70),0,0,0,0,0,0,0,1);
//		client.request(a.toString());
//		try {
//			Thread.sleep(150);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//		
//		}

	}
	
	
	public testUDPClient() {
		client = new UDPClient();
		
		
		gui();
		
		while(true)
		{
			
		
		AProtocol P = new AProtocol();
		P.in_angle=angle;
		P.in_direction=direction;
		P.in_vel=vel;
		client.request(P.toString());
//		client.close();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}
		
		
	}
	
	
	UDPClient client;
	private JSlider slider_angle,slider_vel,slider_direction;
	private JButton button ;
	private int vel = 0  ;	
	private int beep = 0 ;	
	private int direction = 0;	
	private int angle = 90;
	private JLabel label;
	
	
	public void gui()
	{
		System.out.println("Gui launched!");
		JFrame f = new JFrame();
		 slider_angle = new JSlider();
		 slider_angle.setMinimum(50);
		 slider_angle.setMaximum(130);
		 slider_angle.setValue(90);
		 angle = slider_angle.getValue();
		 

		
		 
		
//		 
		 slider_vel = new JSlider();
		 slider_vel.setMinimum(0);
		 slider_vel.setMaximum(255);
		 slider_vel.setValue(0);
		 vel = slider_vel.getValue();
		 
		 label = new JLabel();
		 label.setText("Angle: "+angle);
		 
		 slider_direction = new JSlider();
		 slider_direction.setMinimum(0);
		 slider_direction.setMaximum(1);
		 slider_direction.setValue(0);
		 direction = slider_direction.getValue();
		 
		 
		 
		 
		 slider_direction.addChangeListener(new ChangeListener() {
				
				@Override
				public void stateChanged(ChangeEvent arg0) {
					direction = slider_direction.getValue();
			
				}
			});
		 
		 slider_vel.addChangeListener(new ChangeListener() {
				
				@Override
				public void stateChanged(ChangeEvent arg0) {
					vel = slider_vel.getValue();
			
				}
			});
		 
		 
		 
		 
		 button= new JButton("Beep");
		 
//		 JButton bLeft = new JButton();
//		 bLeft.addActionListener(new ActionListener() {
//			
//			@Override
//			public void actionPerformed(ActionEvent arg0) {
//				if(angle>40)
//					angle-=5;
//				label.setText("Angle: "+angle);
//				label.validate();
//				
//			}
//		});

				 
		 slider_angle.addChangeListener(new ChangeListener() {
				
				@Override
				public void stateChanged(ChangeEvent arg0) {
					
						angle=slider_angle.getValue();
						label.setText("Angle: "+angle);
						label.validate();
				}
			});
		
		 
//		 JButton bRight= new JButton();
		
		 
//		 bRight.addActionListener(new ActionListener() {
//				
//				@Override
//				public void actionPerformed(ActionEvent arg0) {
//			
//			
//					System.out.println("VK_RIGHT "+ angle);
//				if(angle<140)
//					angle+=5;
//				label.setText("Angle: "+angle);
//				label.validate();
//				
//				
//			}
//		});
	
		
		 
		f.setLayout(new GridLayout(3,2));
		f.add(label);
		f.add(slider_angle);
		f.add(new JLabel("Dir"));
		f.add(slider_direction);
		f.add(new JLabel("Vel"));
		f.add(slider_vel);
//		f.add(label);
//		f.add(button);
//		f.add(bLeft);
//		f.add(bRight);
		f.setSize(200,200);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		
	}
	
	
	

}
